package ui.panels;

import javax.swing.JOptionPane;
import manager.GenericDriver;
import ui.windows.RemedySchedulerFrame;

public class ChangePassPanel extends javax.swing.JPanel {

    /** Creates new form ChangePassPanel */
    public ChangePassPanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFrame = mainFrame;
        includeModel();
    }

    private void includeModel() {
        id = mainFrame.getUser().getUserID();
        lbID.setText(mainFrame.getUser().getLoginID());
    }

    private void changePass() {
        String oldPass = new String(txtOldPass.getPassword());
        if (oldPass != null && oldPass.length() != 0) {
            String newPass = new String(txtNewPass.getPassword());
            String confPass = new String(txtConfPass.getPassword());
            if (newPass != null && newPass.length() != 0 && newPass.equals(confPass)) {
                GenericDriver driver = mainFrame.getGenericDriver();
                boolean changed = driver.changePass(id, oldPass, newPass);
                if (changed) {
                    txtOldPass.setText("");
                    txtNewPass.setText("");
                    txtConfPass.setText("");
                    JOptionPane.showMessageDialog(null, "Password changed");
                } else {
                    JOptionPane.showMessageDialog(null, "Password not authenticate", "Error", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "New password not authenticate");
            }
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private int id;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewChangePass = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbID = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtOldPass = new javax.swing.JPasswordField();
        txtNewPass = new javax.swing.JPasswordField();
        txtConfPass = new javax.swing.JPasswordField();
        bChange = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        viewChangePass.setBackground(new java.awt.Color(255, 255, 255));
        viewChangePass.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Change Password", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel1.setText("Old Password:");

        jLabel2.setText("New Password:");

        jLabel3.setText("Login:");

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbID.setText("Value");

        jLabel4.setText("Enter again:");

        javax.swing.GroupLayout viewChangePassLayout = new javax.swing.GroupLayout(viewChangePass);
        viewChangePass.setLayout(viewChangePassLayout);
        viewChangePassLayout.setHorizontalGroup(
            viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(viewChangePassLayout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(36, 36, 36)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtConfPass)
                    .addComponent(lbID)
                    .addComponent(txtOldPass, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                    .addComponent(txtNewPass))
                .addContainerGap(130, Short.MAX_VALUE))
        );
        viewChangePassLayout.setVerticalGroup(
            viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(viewChangePassLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtOldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(viewChangePassLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtConfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        bChange.setText("Change Password");
        bChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bChangeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bChange)
                    .addComponent(viewChangePass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(viewChangePass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bChange)
                .addContainerGap(92, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bChangeActionPerformed
    changePass();
}//GEN-LAST:event_bChangeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bChange;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lbID;
    private javax.swing.JPasswordField txtConfPass;
    private javax.swing.JPasswordField txtNewPass;
    private javax.swing.JPasswordField txtOldPass;
    private javax.swing.JPanel viewChangePass;
    // End of variables declaration//GEN-END:variables
}
